# -*- coding: utf-8 -*-


import arcpy
from arcpy.sa import *
import os
# from arcpy.ia import *

arcpy.CheckOutExtension("Spatial")


inR1_path = u"F:\\bsdata\\Example\\test\\2020.11.16wuhan.tif"
inR2_path = u"F:\\bsdata\\Example\\test\\2020.12.02wuhan.tif"


root_in_test = u"E:\\毕设数据\\Example\\WuhanNDVI\\2020"
root_in_path = u"E:\\毕设数据\\WuhanNDVI"
root_out_path = u"E:\\毕设数据\\WuhanNDVIYearAvg"

def filter(folder):
    for i in os.listdir(folder):
        if i.endswith(".tif"):
            print os.path.join(folder, i)
            yield os.path.join(folder, i)


def calc_avg(inRasterDir, outRaster):
    rs = []
    for i in filter(inRasterDir):
        rs.append(Raster(i))
    out = sum(rs)/len(rs)
    out.save(outRaster)


def run():

    for i in os.listdir(root_in_path):
        in_dir = os.path.join(root_in_path, i)
        out_name = os.path.join(root_out_path, "year-" + i + "-NDVI-avg.tif")
        # print in_dir
        calc_avg(in_dir, out_name)
        print out_name + " OK"



if __name__ == '__main__':
    # calc_avg()
    run()



